package paa.simulator;

import paa.simulator.grafo.PessoaVertex;
import paa.simulator.grafo.simple.UndirectedSimpleGraph;
import paa.simulator.heuristic.Heuristic;
import paa.simulator.model.Model;

public class Simulator {

	private final UndirectedSimpleGraph<PessoaVertex> graph;
	private final Heuristic chooser;
	private final Model model;
	private final int nInitialVertex;
	private int nRounds;

	public Simulator(UndirectedSimpleGraph<PessoaVertex> graph, Heuristic chooser, Model model, int nInitialVertex) {
		this.graph = graph;
		this.chooser = chooser;
		this.model = model;
		this.nInitialVertex = nInitialVertex;
		this.nRounds = 0;
	}
	
	public int run() {
		
		model.initiateGraph(graph);
		int maxEscolher = nInitialVertex;
		int nEscolhidos = chooser.escolheVerticesIniciais(graph, nInitialVertex);
		
		boolean changedOpinions = true;
		while(changedOpinions) {
			changedOpinions = model.executeTurn(graph);
			
			if (nEscolhidos < maxEscolher) {
				nEscolhidos += chooser.refinaEscolha(graph, nInitialVertex - nEscolhidos);
			}
			
			nRounds++;
		}
		
		return nRounds;
	}
}